import { _decorator, AudioSource, Component } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 音效管理器
 */
@ccclass('AudioManager')
export class AudioManager extends Component {
    static Instance: AudioManager;

    @property(AudioSource) bgm: AudioSource = null!;
    @property(AudioSource) sfx_click: AudioSource = null!;
    @property(AudioSource) sfx_chest: AudioSource = null!;
    @property(AudioSource) sfx_win: AudioSource = null!;

    @property(AudioSource) sfx_girl_01: AudioSource = null!;
    @property(AudioSource) sfx_girl_02: AudioSource = null!;
    @property(AudioSource) sfx_girl_03: AudioSource = null!;
    @property(AudioSource) sfx_girl_04: AudioSource = null!;
    @property(AudioSource) sfx_girl_05: AudioSource = null!;
    @property(AudioSource) sfx_girl_total: AudioSource = null!;

    @property(AudioSource) sfx_levelup: AudioSource = null!;
    @property(AudioSource) sfx_common_explosion: AudioSource = null!;
    @property(AudioSource) sfx_common_fire: AudioSource = null!;
    @property(AudioSource) sfx_fortress_cast: AudioSource = null!;
    @property(AudioSource) sfx_victory: AudioSource = null!;

    /** 是否允许播放 */
    @property
    public canPlay: boolean = true;

    onLoad() {
        AudioManager.Instance = this;
    }

    start() {
        this.playBgm(true);
    }

    /** 播放背景音乐 */
    public playBgm(isPlay: boolean = true) {
        if (!this.canPlay) {
            this.bgm.stop();
            return;
        }
        isPlay ? this.bgm?.play() : this.bgm?.stop();
    }

    public playUIClick() {
        if (!this.canPlay) return;
        this.sfx_click.play();
    }

    public playChest() {
        if (!this.canPlay) return;
        this.sfx_chest.play();
    }

    public playWin() {
        if (!this.canPlay) return;
        this.sfx_win.play();
    }


    public playGirlSound(index: number) {
        if (!this.canPlay) return;
        this.sfx_girl_01.stop();
        this.sfx_girl_02.stop();
        this.sfx_girl_03.stop();
        this.sfx_girl_04.stop();
        this.sfx_girl_05.stop();

        // 暂停背景音乐
        this.bgm.pause();

        let sfx: AudioSource | null = null;
        if (index === 1) sfx = this.sfx_girl_01;
        if (index === 2) sfx = this.sfx_girl_02;
        if (index === 3) sfx = this.sfx_girl_03;
        if (index === 4) sfx = this.sfx_girl_04;
        if (index === 5) sfx = this.sfx_girl_05;
        if (sfx) {
            sfx.play();
            // 用 setTimeout 监听播放结束恢复bgm
            setTimeout(() => {
                if (this.canPlay) this.bgm.play();
            }, sfx.duration * 1000);
        }
    }



    public playTotalGirlSound() {
        if (!this.canPlay) return;
        this.sfx_girl_total.play();
    }

    public playLevelUp() {
        if (!this.canPlay) return;
        this.sfx_levelup.play();
    }

    public playCommonExplosion() {
        if (!this.canPlay) return;
        this.sfx_common_explosion.play();
    }

    public playCommonFire() {
        if (!this.canPlay) return;
        this.sfx_common_fire.play();
    }

    public playFortressCast() {
        if (!this.canPlay) return;
        this.sfx_fortress_cast.play();
    }

    public playVictory() {
        if (!this.canPlay) return;
        this.sfx_victory.play();
    }


}

